<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */

if (!defined("Competition_lib_include")){
	define("Competition_lib_include", 1);
	include_once(__Modules_Path."/SelectGambleType.lib.php");
	include_once(__Modules_Path."/Lotto.lib.php");
	include_once(__Modules_Path."/Gamble.lib.php");


/**
 * 載入預設賠率
 * @param	intLID 樂透序號
 * @param	intGID 操盤序號
 * @return	回傳 true -> 成功，false -> 失敗
 */
	function InsertPreOdds($intLID, $intGID){
		global $_Conn, $$_Conn, $_arrGLineNumToApointNum;
		$arrGLineNumToANum = $_arrGLineNumToApointNum[$intLID];

		//$intApointNum = 0;
		//取得該樂透基本資料
		if(!$arrLotto = GetLottoData($intLID))return false;

		switch($arrLotto["LType"]){
			case 2:
				return Insert3DPreOdds($intLID, $intGID, $arrLotto);
				break;

			case 3:
				return Insert7StarPreOdds($intLID, $intGID, $arrLotto);
				break;

			case 4:
				return Insert10MinPreOdds($intLID, $intGID, $arrLotto);
				break;
		}

		$arrTmp = explode("-", $arrLotto["Number"]);
		$intNumStart = intval($arrTmp[0]);
		$intNumEnd = intval($arrTmp[1]);

		//取得該樂透分盤資料
		if(!$arrGLineOdds = GetArrayLottoGambleTypeData($intLID))return false;

		//取得參數
		if(!$arrBP = GetArrayBetParam($intLID, $arrLotto["LType"]))return false;

		$arrOdds = array();
		$arrBT = array();

		//找出 Lotto 的生肖參數，傳回陣列
		$arrAnimals = getAnimalsParams($intLID);
		if($arrAnimals === false)return false;	
		$intMaxAnimals = 0;
		for($i = 1; $i <= 12; $i++){
			if(strlen($arrAnimals[$i]) > strlen($arrAnimals[$intMaxAnimals]))$intMaxAnimals = $i;
		}

		foreach($arrBP as $intBT => $arrAParam){
			$intANum = 0;
			$arrPreOdds = explode(",", $arrAParam[$intANum]["preOdds"]);
			$arrBT[] = $intBT;
			switch($intBT){
				case 1:	//特碼
				case 28:	//特碼46
				case 4:	//正碼
				case 21:	//正1特
				case 22:	//正2特
				case 23:	//正3特
				case 24:	//正4特
				case 25:	//正5特
				case 26:	//正6特
				case 31:	//五不中
					for($intNum = $intNumStart; $intNum <= $intNumEnd; $intNum++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $intNum, $arrPreOdds[0]);
					}
					break;

				case 8:	//三中二
				case 10:	//二中特
				case 7:	//三全中
				case 9:	//二全中
				case 11:	//特串
					if($intBT == 8 || $intBT == 10){
						//輸入三中二之中三預設賠率 或 二中二之中二預設賠率
						//賠率紀錄
						$arrOdds[] = array($intBT, 1, $arrPreOdds[1]);
					}
					//賠率紀錄
					$arrOdds[] = array($intBT, 0, $arrPreOdds[0]);
					break;

				case 12:	 //正碼 1-6 單雙
				case 13:	 //正碼 1-6 大小
				case 27:	 //正碼 1-6 合數單雙
				case 2:	 //特碼單雙
				case 3:	 //特碼大小
				case 5:	 //正碼單雙
				case 6:	 //正碼大小
				case 16:	 //合數單雙
					$intStart = 0;
					$intEnd = 0;
					if($intBT == 12 || $intBT == 13 || $intBT == 27){
						$intStart = 1;
						$intEnd = 6;
					}
					for($i = $intStart; $i <= $intEnd; $i++){
						//採用 A 盤賠率
						$flOdds1 = $arrPreOdds[0]*1;
						$flOdds2 = $arrGLineOdds[$intBT][$intANum][1] - $flOdds1;

						//賠率紀錄 大盤 或 單分
						$arrOdds[] = array($intBT, ($i*10+0), $flOdds1);

						//賠率紀錄 小盤 或 雙分
						$arrOdds[] = array($intBT, ($i*10+1), $flOdds2);
					}
					break;

				case 14:	//正碼 1-6 色波
					//輸入紅波預設賠率
					//載入正碼及特碼預設賠率
					for($i = 1; $i <= 6; $i++){
						//紅波賠率紀錄
						$arrOdds[] = array($intBT, ($i*10), $arrPreOdds[1]);

						for($j = 1; $j <= 2; $j++){
							//賠率紀錄
							$arrOdds[] = array($intBT, ($i*10+$j), $arrPreOdds[0]);
						}
					}
					break;

				case 17:	//特碼色波
					//輸入紅波預設賠率
					//賠率紀錄
					$arrOdds[] = array($intBT, 0, $arrPreOdds[1]);

					for($i = 1; $i <= 2; $i++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;
					
				case 18:	//特碼生肖
				case 19:	//一般生肖
				case 32:	//二肖連-中
				case 33:	//三肖連-中
				case 34:	//四肖連-中
				case 35:	//二肖連-不中
				case 36:	//三肖連-不中
				case 37:	//四肖連-不中
					//輸入特殊生肖預設賠率
					//賠率紀錄
					$arrOdds[] = array($intBT, $intMaxAnimals, $arrPreOdds[1]);

					for($i = 1; $i <= 12; $i++){
						if($intMaxAnimals == $i)continue;
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;

				case 20:	//一般尾數
				case 38:	//二尾連-中
				case 39:	//三尾連-中
				case 40:	//四尾連-中
				case 41:	//二尾連-不中
				case 42:	//三尾連-不中
				case 43:	//四尾連-不中
					//輸入"0"尾數預設賠率
					//賠率紀錄
					$arrOdds[] = array($intBT, 0, $arrPreOdds[1]);

					for($i = 1; $i <= 9; $i++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;

				case 29:	//半波
					//輸入特半波預設賠率
					for($i = 0; $i <= 11; $i++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[$i]);
					}
					break;

				case 30:	//六肖
					//中
					for($i = 1; $i <= 12; $i++){
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					//不中
					for($i = 13; $i <= 24; $i++){
						$arrOdds[] = array($intBT, $i, $arrPreOdds[1]);
					}
					break;
			}
			if(sizeof($arrOdds) > 2000){
				if(!setMultiAddOdds($intLID, $intGID, $arrOdds))return false;
				$arrOdds = array();
			}
		} //switch($intBT){

		if(sizeof($arrOdds) > 0){
			if(!setMultiAddOdds($intLID, $intGID, $arrOdds))return false;
			$arrOdds = array();
		}

		//初始化 TodayGame
		if(!initialTodayGame($intLID, $intGID))return false;
		//產生 TodayGame 檔案
		foreach($arrBT as $intBT){
			if(!MakeTodayGameFile($intLID, $intBT))return false;
		}

		return true;
	}


/**
 * 載入 3D 預設賠率
 * @param	intLID 樂透序號
 * @param	intGID 操盤序號
 * @param	arrLotto 樂透陣列資料
 * @return	回傳 true -> 成功，false -> 失敗
 */
	function Insert3DPreOdds($intLID, $intGID, $arrLotto){
		global $_Conn, $$_Conn, $_arrGLineNumToApointNum;
		$arrGLineNumToANum = $_arrGLineNumToApointNum[$intLID];

		//$intApointNum = 0;
		//取得該樂透分盤資料
		if(!$arrGLineOdds = GetArrayLottoGambleTypeData($intLID))return false;

		//取得參數
		if(!$arrBP = GetArrayBetParam($intLID, $arrLotto["LType"]))return false;
		//$strSQL = "select * from `bet_BetType` where isOff = 0";
		/*$strSQL = "select * from `bet_LottoBetParam` where LottoID = ".$intLID;
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Bet Type Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}*/

		$arrOdds = array();
		$arrBT = array();
		//while($objLottoBT = PMA_mysql_fetch_object($RS)){
		foreach($arrBP as $intBT => $arrAParam){
			$intANum = 0;
			$arrPreOdds = explode(",", $arrAParam[$intANum]["preOdds"]);

			$arrBT[] = $intBT;
			switch($intBT){
				case 51:	//定膽個位
				case 54:	//定膽十位
				case 57:	//定膽百位
				case 60:	//定膽不定位
				case 69:	//組 3轉直-5
				case 70:	//組 3轉直-6
				case 71:	//組 3轉直-7
				case 72:	//組 3轉直-8
				case 73:	//組 6轉直-4
				case 74:	//組 6轉直-5
				case 75:	//組 6轉直-6
				case 76:	//組 6轉直-7
				case 77:	//組 6轉直-8
				case 91:	//合值
					$intNumStart = 0;
					$intNumEnd = 9;
					for($intNum = $intNumStart; $intNum <= $intNumEnd; $intNum++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $intNum, $arrPreOdds[0]);
					}
					break;

				case 52:	 //定膽個位單雙
				case 53:	 //定膽個位大小
				case 55:	 //定膽十位單雙
				case 56:	 //定膽十位大小
				case 58:	 //定膽百位單雙
				case 59:	 //定膽百位大小
				case 62:	 //定膽和值單雙
				case 63:	 //定膽和值大小
				case 84:	 //定膽個位質合
				case 85:	 //定膽十位質合
				case 86:	 //定膽百位質合
					$intStart = 0;
					$intEnd = 0;

					for($i = $intStart; $i <= $intEnd; $i++){
						//採用 A 盤賠率
						$flOdds1 = $arrPreOdds[0]*1;
						$flOdds2 = $arrGLineOdds[$intBT][$intANum][1] - $flOdds1;

						//賠率紀錄 大盤 或 單分
						$arrOdds[] = array($intBT, ($i*10+0), $flOdds1);

						//賠率紀錄 小盤 或 雙分
						$arrOdds[] = array($intBT, ($i*10+1), $flOdds2);
					}
					break;

				case 64:	//雙飛定位十個
				case 65:	//雙飛定位百個
				case 66:	//雙飛定位百十
					$intNumStart = 0;
					$intNumEnd = 99;
					for($i = $intNumStart; $i <= $intNumEnd; $i++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;

				case 61:	//定膽和值
					$intNumStart = 6;
					$intNumEnd = 21;
					for($i = $intNumStart; $i <= $intNumEnd; $i++){
						$intANum = (int)$arrGLineNumToANum[$intBT][$i];
						$arrPreOdds = explode(",", $arrAParam[$intANum]["preOdds"]);
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;

				case 68:	//組 3全包
				case 78:	//3x3x3
				case 79:	//4x4x4
				case 80:	//5x5x5
				case 81:	//6x6x6
				case 82:	//7x7x7
				case 83:	//中 3 保 3
					$intNumStart = 0;
					$intNumEnd = 0;
					for($i = $intNumStart; $i <= $intNumEnd; $i++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;

				case 67:	//雙飛不定位
					$intNumStart = 0;
					$intNumEnd = 99;
					for($i = $intNumStart; $i <= $intNumEnd; $i++){
						if(floor($i / 10) >  ($i % 10))continue;
						$intANum = (int)$arrGLineNumToANum[$intBT][$i];
						$arrPreOdds = explode(",", $arrAParam[$intANum]["preOdds"]);
						//$strPreOdds = (floor($i / 10) == ($i % 10))?$arrPreOdds[1]:$arrPreOdds[0];
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;

				case 87:	//定膽單雙過關
				case 88:	//定膽大小過關
				case 89:	//定膽質合過關
					$intNumStart = 0;
					$intNumEnd = 7;
					for($i = $intNumStart; $i <= $intNumEnd; $i++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;

				case 90:	//跨度
					$intNumStart = 0;
					$intNumEnd = 9;
					for($i = $intNumStart; $i <= $intNumEnd; $i++){
						$intANum = (int)$arrGLineNumToANum[$intBT][$i];
						$arrPreOdds = explode(",", $arrAParam[$intANum]["preOdds"]);
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;

				case 92:	//三定位
					$intNumStart = 0;
					$intNumEnd = 999;
					for($i = $intNumStart; $i <= $intNumEnd; $i++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;

				case 93:	//三字現
					$intNumStart = 0;
					$intNumEnd = 999;
					for($i = $intNumStart; $i <= $intNumEnd; $i++){
						if($i < 100){
							if(floor($i / 10) > ($i % 10))continue;
						}else{
							if(floor($i / 10) > ($i % 100))continue;
							if(floor(($i % 100) / 10) > ($i % 10))continue;
						}
						/*$strPreOdds = $arrPreOdds[0];
						if(floor($i / 10) == ($i % 100)){
							$strPreOdds = $arrPreOdds[2];
						}elseif(floor($i / 100) == floor(($i % 100) / 10) || floor(($i % 100) / 10) == $i % 10){
							$strPreOdds = $arrPreOdds[1];
						}*/
						$intANum = (int)$arrGLineNumToANum[$intBT][$i];
						$arrPreOdds = explode(",", $arrAParam[$intANum]["preOdds"]);
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;
			} //switch($intBT)

			if(sizeof($arrOdds) > 2000){
				if(!setMultiAddOdds($intLID, $intGID, $arrOdds))return false;
				$arrOdds = array();
			}
		} //while($objLottoBT = PMA_mysql_fetch_object($RS))

		if(sizeof($arrOdds) > 0){
			if(!setMultiAddOdds($intLID, $intGID, $arrOdds))return false;
			$arrOdds = array();
		}

		//初始化 TodayGame
		if(!initialTodayGame($intLID, $intGID))return false;
		//產生 TodayGame 檔案
		foreach($arrBT as $intBT){
			if(!MakeTodayGameFile($intLID, $intBT))return false;
		}
		return true;
	}


/**
 * 載入 7Star 預設賠率
 * @param	intLID 樂透序號
 * @param	intGID 操盤序號
 * @param	arrLotto 樂透陣列資料
 * @return	回傳 true -> 成功，false -> 失敗
 */
	function Insert7StarPreOdds($intLID, $intGID, $arrLotto){
		global $_Conn, $$_Conn, $_arrGLineNumToApointNum;
		$arrGLineNumToANum = $_arrGLineNumToApointNum[$intLID];

		//$intApointNum = 0;
		//取得該樂透分盤資料
		if(!$arrGLineOdds = GetArrayLottoGambleTypeData($intLID))return false;

		if(!$arrBP = GetArrayBetParam($intLID, $arrLotto["LType"]))return false;
		//$strSQL = "select * from `bet_BetType` where isOff = 0";
		/*$strSQL = "select * from `bet_LottoBetParam` where LottoID = ".$intLID;
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Bet Type Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}*/

		$arrOdds = array();
		$arrBT = array();
		//while($objLottoBT = PMA_mysql_fetch_object($RS)){
		foreach($arrBP as $intBT => $arrAParam){
			$intANum = 0;
			$arrPreOdds = explode(",", $arrAParam[$intANum]["preOdds"]);
			//$arrPreOdds = split(",", $objLottoBT -> preOdds);
			$arrBT[] = $intBT;
			switch($intBT){
				case 1:		//定位千百
				case 2:		//定位千十
				case 3:		//定位千個
				case 4:		//定位百十
				case 5:		//定位百個
				case 6:		//定位十個
					$intNumStart = 0;
					$intNumEnd = 99;
					for($intNum = $intNumStart; $intNum <= $intNumEnd; $intNum++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $intNum, $arrPreOdds[0]);
					}
					break;

				case 7:		//三定位-口口口X
				case 8:		//三定位-口口X口
				case 9:		//三定位-口X口口
				case 10:	//三定位-X口口口
					$intNumStart = 0;
					$intNumEnd = 999;
					for($intNum = $intNumStart; $intNum <= $intNumEnd; $intNum++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $intNum, $arrPreOdds[0]);
					}
					break;

				case 11:	//四定位
					$intNumStart = 0;
					$intNumEnd = 9999;
					for($intNum = $intNumStart; $intNum <= $intNumEnd; $intNum++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $intNum, $arrPreOdds[0]);
					}
					break;

				case 12:	//二字現
					$intNumStart = 0;
					$intNumEnd = 99;
					for($i = $intNumStart; $i <= $intNumEnd; $i++){
						if(floor($i / 10) >  ($i % 10))continue;
						$intANum = (int)$arrGLineNumToANum[$intBT][$i];
						$arrPreOdds = explode(",", $arrAParam[$intANum]["preOdds"]);
						//$strPreOdds = (floor($i / 10) == ($i % 10))?$arrPreOdds[1]:$arrPreOdds[0];
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;

				case 13:	//三字現
					$intNumStart = 0;
					$intNumEnd = 999;
					for($i = $intNumStart; $i <= $intNumEnd; $i++){
						if($i < 100){
							if(floor($i / 10) > ($i % 10))continue;
						}else{
							if(floor($i / 10) > ($i % 100))continue;
							if(floor(($i % 100) / 10) > ($i % 10))continue;
						}
						/*$strPreOdds = $arrPreOdds[0];
						if(floor($i / 10) == ($i % 100)){
							$strPreOdds = $arrPreOdds[2];
						}elseif(floor($i / 100) == floor(($i % 100) / 10) || floor(($i % 100) / 10) == $i % 10){
							$strPreOdds = $arrPreOdds[1];
						}*/
						$intANum = (int)$arrGLineNumToANum[$intBT][$i];
						$arrPreOdds = explode(",", $arrAParam[$intANum]["preOdds"]);
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;

				case 14:	 //單雙
				case 15:	 //大小
					//0 -> 千, 1 -> 百, 2 -> 十, 3 -> 個
					$intStart = 0;
					$intEnd = 3;

					for($i = $intStart; $i <= $intEnd; $i++){
						//採用 A 盤賠率
						$flOdds1 = $arrPreOdds[0]*1;
						$flOdds2 = $arrGLineOdds[$intBT][$intANum][1] - $flOdds1;

						//賠率紀錄 大盤 或 單分
						$arrOdds[] = array($intBT, ($i*10+0), $flOdds1);

						//賠率紀錄 小盤 或 雙分
						$arrOdds[] = array($intBT, ($i*10+1), $flOdds2);
					}
					break;

				case 16:	 //合數單雙
				case 17:	 //合數大小
					//0 -> 千百, 1 -> 千十, 2 -> 千個, 3 -> 百十, 4 -> 百個, 5 -> 十個
					$intStart = 0;
					$intEnd = 5;

					for($i = $intStart; $i <= $intEnd; $i++){
						//採用 A 盤賠率
						$flOdds1 = $arrPreOdds[0]*1;
						$flOdds2 = $arrGLineOdds[$intBT][$intANum][1] - $flOdds1;

						//賠率紀錄 大盤 或 單分
						$arrOdds[] = array($intBT, ($i*10+0), $flOdds1);

						//賠率紀錄 小盤 或 雙分
						$arrOdds[] = array($intBT, ($i*10+1), $flOdds2);
					}
					break;

				case 18:		//定位千
				case 19:		//定位百
				case 20:		//定位十
				case 21:		//定位個
				case 22:		//一字
					$intNumStart = 0;
					$intNumEnd = 9;
					for($intNum = $intNumStart; $intNum <= $intNumEnd; $intNum++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $intNum, $arrPreOdds[0]);
					}
					break;
			} //switch($intBT)

			if(sizeof($arrOdds) > 2000){
				if(!setMultiAddOdds($intLID, $intGID, $arrOdds))return false;
				$arrOdds = array();
			}
		} //while($objLottoBT = PMA_mysql_fetch_object($RS))

		if(sizeof($arrOdds) > 0){
			if(!setMultiAddOdds($intLID, $intGID, $arrOdds))return false;
			$arrOdds = array();
		}

		//初始化 TodayGame
		if(!initialTodayGame($intLID, $intGID))return false;
		//產生 TodayGame 檔案
		foreach($arrBT as $intBT){
			if(!MakeTodayGameFile($intLID, $intBT))return false;
		}
		return true;
	}


/**
 * 載入 10Min 預設賠率
 * @param	intLID 樂透序號
 * @param	intGID 操盤序號
 * @param	arrLotto 樂透陣列資料
 * @return	回傳 true -> 成功，false -> 失敗
 */
	function Insert10MinPreOdds($intLID, $intGID, $arrLotto){
		global $_Conn, $$_Conn, $_arrGLineNumToApointNum;
		$arrGLineNumToANum = $_arrGLineNumToApointNum[$intLID];

		//$intApointNum = 0;
		//取得該樂透分盤資料
		print "A1";
		if(!$arrGLineOdds = GetArrayLottoGambleTypeData($intLID))return false;
		print "A2";
		if(!$arrBP = GetArrayBetParam($intLID, $arrLotto["LType"]))return false;
		print "A3";
		$arrOdds = array();
		$arrBT = array();
		foreach($arrBP as $intBT => $arrAParam){
			$intANum = 0;
			$arrPreOdds = explode(",", $arrAParam[$intANum]["preOdds"]);
			$arrBT[] = $intBT;
			switch($intBT){
				case 1:		//特碼
				case 5:		//平碼一
				case 6:		//平碼二
				case 7:		//平碼三
				case 8:		//平碼四
				case 15:	//連碼一中一
				case 16:	//連碼二中二
				case 17:	//連碼三中二
				case 18:	//連碼三中三
				case 19:	//連碼四中三
				case 20:	//連碼四中四
				case 21:	//連碼五中三
				case 22:	//連碼五中四
				case 23:	//連碼五中五
				case 24:	//連碼特串
				case 25:	//二不中
				case 26:	//三不中
				case 27:	//四不中
				case 28:	//五不中
					$intNumStart = 1;
					$intNumEnd = 21;
					for($intNum = $intNumStart; $intNum <= $intNumEnd; $intNum++){
						//賠率紀錄
						$arrOdds[] = array($intBT, $intNum, $arrPreOdds[0]);
					}
					break;

				case 11:	 //平碼 1-4 單雙
				case 12:	 //平碼 1-4 大小
				case 13:	 //平碼 1-4 合數單雙
				case 2:	 //特碼單雙
				case 3:	 //特碼大小
				case 4:	 //特碼合數單雙
				case 9:	 //總和單雙
				case 10:	 //總和大小
					$intStart = 0;
					$intEnd = 0;
					if($intBT == 11 || $intBT == 12 || $intBT == 13){
						$intStart = 1;
						$intEnd = 4;
					}
					for($i = $intStart; $i <= $intEnd; $i++){
						//採用 A 盤賠率
						$flOdds1 = $arrPreOdds[0]*1;
						$flOdds2 = $arrGLineOdds[$intBT][$intANum][1] - $flOdds1;

						//賠率紀錄 大盤 或 單分
						$arrOdds[] = array($intBT, ($i*10+0), $flOdds1);

						//賠率紀錄 小盤 或 雙分
						$arrOdds[] = array($intBT, ($i*10+1), $flOdds2);
					}
					break;

				case 29:	//一般尾數
				case 30:	//二尾連-中
				case 31:	//三尾連-中
				case 32:	//四尾連-中
				case 33:	//二尾連-不中
				case 34:	//三尾連-不中
				case 35:	//四尾連-不中
					//輸入"1"尾數預設賠率
					//賠率紀錄
					$arrOdds[] = array($intBT, 1, $arrPreOdds[1]);

					for($i = 0; $i <= 9; $i++){
						if($i == 1)continue;
						//賠率紀錄
						$arrOdds[] = array($intBT, $i, $arrPreOdds[0]);
					}
					break;
			} //switch($intBT)

			if(sizeof($arrOdds) > 2000){
				if(!setMultiAddOdds($intLID, $intGID, $arrOdds))return false;
				$arrOdds = array();
			}
		} //while($objLottoBT = PMA_mysql_fetch_object($RS))

		if(sizeof($arrOdds) > 0){
			if(!setMultiAddOdds($intLID, $intGID, $arrOdds))return false;
			$arrOdds = array();
		}
		print "A5";
		//初始化 TodayGame
		if(!initialTodayGame($intLID, $intGID))return false;
		//產生 TodayGame 檔案
		foreach($arrBT as $intBT){
			if(!MakeTodayGameFile($intLID, $intBT))return false;
		}
		return true;
	}


/**
 * 產生 Gamble 的設定檔
 * @param	intLID 樂透序號
 * @return	回傳 true -> 成功，false -> 失敗
 * 產生檔案 /common/todaygame/Gamble?x.inc.php
 * x -> 樂透序號
 */
	function MakeGambleFile($intLID){
		global $_Conn, $$_Conn;

		$arrGamble = getNewGambleData($intLID, 0);
		if($arrGamble === false)return false;

		if(sizeof($arrGamble) > 0){
			$arrFName = array();
			while(list($k, $v) = each($arrGamble)){
				$arrFName[] = "\"".$k."\"=>\"".$v."\"";
			}
			$strCon .= "$"."_arrGamble[".$arrGamble["ID"]."]=array(".join(",", $arrFName).");\n";
		}

		$strCon = "<"."?"."php\n".$strCon."?".">";
		$strFile = __Common_Path."/todaygame/Gamble".$intLID.".inc.php";
		if(!writeFile($strFile, $strCon, "w"))return false;

		return true;
	}

/**
 * 更改注單結帳日期
 * @param	intGID 操盤序號
 * @param	strGDate 結帳日期
 * @return	回傳 true -> 成功，false -> 失敗
 */
	function ChangeWagerGameDate($intGID, $strGDate){
		global $_ConnMaster;

		$strSQL = "select ID from `bet_BetList` where GambleID = ".$intGID;
		if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Find Wager Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($_ConnMaster)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		if(PMA_mysql_num_rows($RS) == 0)return true;

		$arrID = array();
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrID[] = $Rows -> ID;
		}

		$strSQL = "update `bet_BetListContent` set GameDate = '".$strGDate."' where ID in (".join(",", $arrID).")";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Change Wager Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($_ConnMaster)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		$strSQL = "update `bet_BetList` set PayupDate = '".$strGDate."' where ID in (".join(",", $arrID).")";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Change Wager Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($_ConnMaster)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		$strSQL = "update `bet_BetListTmp` set PayupDate = '".$strGDate."' where ID in (".join(",", $arrID).")";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Change Wager Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($_ConnMaster)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}
	}


/**
 * 刪除已結束賽程昨日以前之暫存資料
 * @return	回傳 true -> 成功，false -> 失敗
 */
	function rmCompetitionEndLastDayData($intLastDay = 1){
		global $_ConnMaster;

		$arrD = explode("-", date("Y-m-d"));
		$strLastDay = date("Y-m-d", mktime(0, 0, 0, $arrD[1], $arrD[2] - $intLastDay, $arrD[0]));

		$strSQL = "select DISTINCT a.ID as ID, a.LottoID as LottoID from `bet_Gamble` a, `bet_BetListTmp` b where a.isEnd = 1 and a.ID = b.GambleID and b.PayupDate <= '".$strLastDay."'";
		if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Find Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		if(PMA_mysql_num_rows($RS) == 0)return true;

		$arrGID = array();
		$arrLID = array();
		//$isL4 = false;
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrGID[$Rows -> ID] = $Rows -> ID;
			$arrLID[$Rows -> LottoID][$Rows -> ID] = $Rows -> ID;
			//if($Rows -> LottoID == 4)$isL4 = true;
		}
		$strGID = join(",", $arrGID);
		$strSQL = "delete from `bet_BetListTmp` where GambleID in (".$strGID.")";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Del Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		$strSQL = "OPTIMIZE TABLE `bet_BetListTmp`";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "OPTIMIZE table Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		/*$strSQL = "delete from `bet_ParlaysNumberEven` where `GambleID` in (".$strGID.")";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Delete Parlays Data Error!!\n";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		$strSQL = "OPTIMIZE TABLE `bet_ParlaysNumberEven`";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "OPTIMIZE table Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}*/
		//if($isL4 == false)return true;

		//清除各階層暫存注單統計資料
		foreach($arrLID as $intLID => $arrGID){
			$strGID = implode(",", $arrGID);

			$strSQL = "delete from `bet_RealTodayGame".$intLID."` where `GambleID` in (".$strGID.")";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				$GLOBALS["errormessage"] = "Delete bet_RealTodayGame".$intLID." Data Error!!\n";
				$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLine: ".__LINE__.",\nPage: ".__FILE__;
				return false;
			}
			$strSQL = "OPTIMIZE TABLE `bet_RealTodayGame".$intLID."`";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				$GLOBALS["errormessage"] = "OPTIMIZE table ".$intLID." Error!!";
				$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLine: ".__LINE__.",\nPage: ".__FILE__;
				return false;
			}
		}
		return true;
	}


/**
 * 將未結帳之 Gamble 改為已結帳
 * @param	intLID 樂透序號
 * @return	回傳 true -> 成功，false -> 失敗
 */
	function setGameEnd($intLID){
		global $_ConnMaster;

		$strSQL = "update `bet_Gamble` set isEnd = 1 where isEnd = 0 and LottoID = ".$intLID;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "End This Game Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		return true;
	}
//-------------------------------------------------------//
//Competition_lib End
}
?>